<style lang="scss">
.v-bg {
    width: 750rpx;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    image {
        width: 100%;
        height: 100%;
    }
}
.center-box {
    width: 600rpx;
    min-height: 700rpx;
    background: #fff;
    position: fixed;
    border-radius: 10rpx;
    left: 75rpx;
    top: 50%;
    margin-top: -350rpx;
    font-size: 30rpx;
    view {
        width: 500rpx;
        margin: 30rpx auto;
        text-align: center;
    }
    .line {
        height: 150rpx;
        line-height: 150rpx;
        background: #f1f1f1;
        color: #ff6702;
        image {
            width: 50rpx;
            height: 50rpx;
            position: relative;
            top: 10rpx;
            left: -10rpx;
        }
        text {
            width: 200rpx;
            text-align: center;
            display: inline-block;
        }
    }
}
</style>
<template>
    <view>
        <view class="v-bg"><image src="/static/img/timg.png" alt="" /></view>
        <view class="center-box" v-show="showChoose">
            <view class="title">选择身份进入好阿姨</view>
            <view class="line" @click="forward(2)">
                <image src="/static/img/yzhu01.png" mode="widthFix" />
                <text>我是业主</text>
            </view>
            <view class="line" @click="forward(3)">
                <image src="/static/img/gs01.png" mode="widthFix" />
                <text>我是家政公司</text>
            </view>
            <view class="line" @click="forward(1)">
                <image src="/static/img/ayi01.png" mode="widthFix" />
                <text>我是阿姨</text>
            </view>
        </view>
    </view>
</template>
<script>
const app = getApp();
// 这里写你的js路径
var QQMapWX = require('../../util/qqmap-wx-jssdk.min.js');
var qqmapsdk;
import { logina, loginb } from '../../api/api';
export default {
    data() {
        return {
            showChoose: false,
            role: '',
            openid: '',
            name: '',
            sex: '',
            headimg: '',
            sessionkey: '',
            province: '',
            city: '',
            info: '',
            district: '',
            street: '',
            city_id: '',
            district_id: '',
            province_id: '',
            latitude: '',
            longitude: '',
            userInfo: ''
        };
    },
    methods: {
        forward(role) {
            this.$setRole(role);
            this.wxlogin()
            //this.jumpHome();
        },
        jumpHome() {
            let role = this.$global.role;
            if (role == 1) {
                uni.reLaunch({
                    url: '../ay/ayhome'
                });
            } else if (role == 2) {
                uni.reLaunch({
                    url: '../yz/yzhome'
                });
            } else if (role == 3) {
                uni.reLaunch({
                    url: '../gs/gshome'
                });
            }
        },
        async wxlogin() {
            let that = this;
            wx.login({
                success: res => {
                    console.log('wxlogin', res);
                    that.code = res.code;
                    that.legacy_login();
                }
            });
        },
        async legacy_login() {
            let type = this.$global.role;
            let res = await logina({
                code: this.code,
                loadingTip: '登录中'
            });

            let sessionkey = res.session_key;
            this.$setOpenid(res.openid);
            if (res.openid) {
                this.jumpHome()
                return;
            }
            let param = {
                openid: this.$global.openid,
                type: this.$global.role,
                //name: this.userInfo.nickName,
                //sex: this.userInfo.gender == 1 ? '男' : '女',
                //headimg: this.userInfo.avatarUrl,
                sessionkey: sessionkey
            };
            let userInfo = uni.getStorageSync('userInfo');
            if (userInfo) {
                param.name = userInfo.nickName;
                param.sex = userInfo.gender == 1 ? '男' : '女';
                param.headimg = userInfo.avatarUrl;
            }
            res = await loginb(param).then(res => {
                uni.setStorageSync('userdetails', res.userinfo);
                this.$setIdentification(res.userinfo.identification);
                this.$setSessionKey(res.userinfo.sessionkey);

                let url = '';
                if (type == 2) {
                    url = '../yz/yzhome?type=2';
                } else if (type == 1) {
                    url = '../ay/ayhome?type=1';
                } else if (type == 3) {
                    if (res.userinfo.identification == 0) {
                        uni.navigateTo({
                            url: 'index?type=' + type
                        });
                        return;
                    } else {
                        url = '../gs/gshome?type=' + type;
                    }
                }
                uni.reLaunch({
                    url: url
                });
            });
        },
        getUserLocation() {
            let vm = this;
            // #ifdef APP-PLUS || H5
            return;
            // #endif
            wx.getSetting({
                success: res => {
                    // console.log("getSetting");
                    // console.log(res)
                    // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
                    // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
                    // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
                    if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
                        wx.showModal({
                            title: '请求授权当前位置',
                            content: '需要获取您的地理位置，请确认授权',
                            success(res) {
                                if (res.cancel) {
                                    wx.showToast({
                                        title: '拒绝授权',
                                        icon: 'none',
                                        duration: 1000
                                    });
                                } else if (res.confirm) {
                                    wx.openSetting({
                                        success(dataAu) {
                                            if (dataAu.authSetting['scope.userLocation'] == true) {
                                                wx.showToast({
                                                    title: '授权成功',
                                                    icon: 'success',
                                                    duration: 1000
                                                });
                                                //再次授权，调用wx.getLocation的API
                                                vm.getLocation();
                                            } else {
                                                wx.showToast({
                                                    title: '授权失败',
                                                    icon: 'none',
                                                    duration: 1000
                                                });
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    } else if (res.authSetting['scope.userLocation'] == undefined) {
                        //调用wx.getLocation的API
                        vm.getLocation();
                    } else {
                        //调用wx.getLocation的API
                        vm.getLocation();
                    }
                }
            });
        },
        // 微信获得经纬度
        getLocation: function() {
            let vm = this;
            wx.getLocation({
                type: 'gcj02', //'wgs84',
                success: function(res) {
                    // console.log("getLocation");
                    // console.log(res)
                    var latitude = res.latitude;
                    var longitude = res.longitude;
                    var speed = res.speed;
                    var accuracy = res.accuracy;
                    vm.$global.latitude = latitude;
                    vm.$global.longitude = longitude;
                    vm.getLocal(latitude, longitude);
                },
                fail: function(res) {
                    console.log('fail', res);
                }
            });
        },
        // 获取当前地理位置
        getLocal(latitude, longitude) {
            let vm = this;
            qqmapsdk.reverseGeocoder({
                location: {
                    latitude: latitude,
                    longitude: longitude
                },
                success(res) {
                    // console.log("getLocal");
                    // console.log(res);
                    let province = res.result.ad_info.province;
                    let city = res.result.ad_info.city;
                    let district = res.result.ad_info.district;
                    let province_id = res.result.ad_info.city_code;
                    let city_id = res.result.ad_info.city_code;
                    let district_id = res.result.ad_info.adcode;
                    let info = JSON.stringify(res.result.ad_info);
                    wx.setStorageSync('district', district);
                    wx.setStorageSync('adcode', district_id);
                    vm.$global.district = district;
                    vm.$global.adcode = district_id;

                    (this.province = province),
                        (this.city = city),
                        (this.district = district),
                        (this.latitude = latitude),
                        (this.longitude = longitude),
                        (this.city_id = city_id),
                        (this.district_id = district_id),
                        (this.info = info);
                    //   wx.showModal({
                    //       title: '位置信息',
                    //     content: district+'--'+district_id,
                    //     success: function (res) {

                    //     }
                    //   })
                },
                fail(res) {
                    console.log('fail');
                    console.log(res);
                },
                complete(res) {
                    // console.log(res);
                }
            });
        }
    },
    onShow() {
        let vm = this;
        vm.getUserLocation();
    },
    onLoad(opt) {
        qqmapsdk = new QQMapWX({
            key: 'YU5BZ-CNQKV-MMJPA-UCWQ4-HRFH2-W5B46'
        });

        if (opt.force) {
            this.$setSessionKey('');
            this.$setIdentification(0);
            uni.clearStorageSync('userdetails');
            uni.clearStorageSync('userbasic');
            this.showChoose = true;
        }

        if (opt.type) {
            let t = parseInt(opt.type);
            if (t != this.$global.role) {
                this.$setSessionKey('');
                this.$setIdentification(0);
                uni.clearStorageSync('userdetails');
                uni.clearStorageSync('userbasic');
            }
            this.$setRole(opt.type);
        }

        // if (this.$global.devEnv) {
        //     this.showChoose = true;
        // }
        if (this.$global.role) {
            if (!this.showChoose) {
                this.wxlogin();
                //this.jumpHome();
            }
        } else {
            this.showChoose = true;
        }
    }
};
</script>
